package com.tenbent.product.center.insures.action;

import com.tenbent.product.base.action.BaseController;
import com.tenbent.product.base.constants.WebConstans;
import com.tenbent.product.base.exception.TenbentApiNotFoundException;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.web.Page;
import com.tenbent.product.center.insures.bo.Product;
import com.tenbent.product.center.insures.dto.ProductDto;
import com.tenbent.product.center.insures.dto.search.ProductSearchPageDto;
import com.tenbent.product.center.insures.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 产品控制器层
 *
 * @author Randy
 *
 *         Created by ThinkPad on 2017/8/21.
 */
@RestController
public class ProductController extends BaseController {

	public static final Logger logger = LoggerFactory.getLogger(ProductController.class);

	public static class Url {
		/** 添加产品 */
		public static final String save = "/api/product/save";
		/** 修改产品 */
		public static final String update = "/api/product/update";
		/** 删除产品 */
		public static final String delete = "/api/product/delete/{id}";
		/** 根据产品code查询 */
		public static final String queryByCode = "/api/product/query_by_code";
		/** 产品综合查询 */
		public static final String query = "/api/product/query";
	}

	@Autowired
	@Qualifier("productServiceImpl")
	private ProductService productService;

	@RequestMapping(value = Url.save, method = { RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseStatus(HttpStatus.CREATED)
	public ProductDto saveApi(@Valid @RequestBody ProductDto productDto, Errors errors,
			@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {

		processErrors(errors);
		settingSaveManager(productDto, managerId);
		if (productDto.getStatus() != null)
			productDto.setStatus(2);
		Product product = productDto.transToBo();
		productService.add(product);
		return new ProductDto(product);
	}

	@RequestMapping(value = Url.update, method = {
			RequestMethod.PUT }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ProductDto updateApi(@Valid @RequestBody ProductDto productDto, Errors errors,
			@ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		if (productDto.getId() == null) {
			errors.rejectValue("id", "product.id.not.empty", getMessage("product.id.not.empty", null));
		}
		processErrors(errors);
		settingModiferManager(productDto, managerId);
		Product product = productDto.transToBo();
		productService.update(product);
		return new ProductDto(product);
	}

	@RequestMapping(value = Url.delete, method = {
			RequestMethod.DELETE }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public void deleteApi(@PathVariable Integer id, @ModelAttribute(WebConstans.BIZ_MANAGER_ID) Integer managerId) {
		productService.deleteLogic(id, managerId);
	}

	@RequestMapping(value = Url.query, method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Page<ProductDto> query(ProductSearchPageDto searchPageDto) {

		PageResult<Product> pageResult = productService.queryPageObj(searchPageDto.transToPageable());
		return new Page(change.changesToDTOPage(pageResult, ProductDto.class));
	}

	@RequestMapping(value = Url.queryByCode, method = {
			RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ProductDto queryByCode(@RequestParam(name = "pdCode") String pdCode) {

		ProductDto product = productService.findProductByCode(pdCode);
		if (product == null) {
			throw new TenbentApiNotFoundException(getMessage("product.pd.code.not.exits", null));
		}
		return product;
	}
}
